package com.group.Listener;

import java.util.HashMap;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.group.pojo.GUser;

@WebListener
public class SessionListener implements HttpSessionListener {
	/**  
     * 该HashMap以用户名-HttpSession对象存储一个账号只能被一个人登陆的信息。  
     */  
    public static HashMap<String,HttpSession> sessionMap = new HashMap<String,HttpSession>();  

    public SessionListener() {
    }
    public void sessionCreated(HttpSessionEvent se)  {
    	HttpSession session = se.getSession(); 
    }
    public void sessionDestroyed(HttpSessionEvent se)  {
    	 HttpSession session = se.getSession();  
         delSession( session );  
    }
	
    public static synchronized void delSession(HttpSession session){  
        if(session != null){  
            // 删除单一登录中记录的变量  
            if(session.getAttribute( "SESSION_USER" ) != null){  
                GUser user = (GUser) session.getAttribute( "SESSION_USER" );  
                SessionListener.sessionMap.remove( user.getUserID());  
            }  
        }  
    } 
}
